/*
 * @lc app=leetcode.cn id=1417 lang=typescript
 *
 * [1417] 重新格式化字符串
 */

// @lc code=start
function reformat(s: string): string {
    const numList = [...Array(10).keys()].map((i) => i.toString());
    let num: string[] = [];
    let char: string[] = [];
    for (let i = 0; i < s.length; i++) {
        const element = s[i];
        if (numList.includes(element)) {
            num.push(element);
        } else {
            char.push(element);
        }
    }
    const diff = Math.abs(char.length - num.length);
    if (diff !== 1 && diff !== 0) return "";

    let result = "";
    while (num.length && char.length) {
        result +=
            char.length > num.length
                ? char.pop()! + num.pop()!
                : num.pop()! + char.pop()!;
    }
    result += (char.length > num.length ? char.pop() : num.pop()!) || "";
    return result;
}
// @lc code=end
